﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Chapter11.Code02.Code02.Snippet10
{
    public class AddClass1
    {
        public int Val;

        public static AddClass1 operator +(AddClass1 op1)
        {
            AddClass1 returnVal = new AddClass1();
            returnVal.Val += op1.Val;
            return returnVal;
        }

        public static AddClass1 operator -(AddClass1 op1)
        {
            AddClass1 returnVal = new AddClass1();
            returnVal.Val -= op1.Val;
            return returnVal;
        }

        public static AddClass1 operator +(AddClass1 op1, AddClass1 op2)
        {
            AddClass1 returnVal = new AddClass1();
            returnVal.Val = op1.Val + op2.Val;
            return returnVal;
        }

        public static AddClass3 operator +(AddClass1 op1, AddClass2 op2)
        {
            AddClass3 returnVal = new AddClass3();
            returnVal.Val = op1.Val + op2.Val;
            return returnVal;
        }

        public static AddClass3 operator +(AddClass2 op1, AddClass1 op2)
        {
            return op2 + op1;
        }

        public static bool operator >=(AddClass1 op1, AddClass1 op2)
        {
            return (op1.Val >= op2.Val);
        }

        public static bool operator <=(AddClass1 op1, AddClass1 op2)
        {
            return (op1.Val >= op2.Val);
        }

        public static bool operator >(AddClass1 op1, AddClass1 op2)
        {
            return (op1.Val > op2.Val);
        }

        public static bool operator <(AddClass1 op1, AddClass1 op2)
        {
            return !(op1 > op2);
        }

        public static bool operator ==(AddClass1 op1, AddClass1 op2)
        {
            return op1.Val == op2.Val;
        }

        public static bool operator !=(AddClass1 op1, AddClass1 op2)
        {
            return !(op1 == op2);
        }

        public override bool Equals(object op1)
        {
            //if (op1.GetType() == typeof(AddClass1))
            if (op1 is AddClass1)
            {
                return Val == ((AddClass1)op1).Val;
            }
            else
            {
                throw new ArgumentException("cannot compare AddClass1 object with objects of type " + op1.GetType().ToString());
            }
        }

        public override int GetHashCode()
        {
            return Val;
        }
    }
}
